<?php

namespace addons\Patrol\api\modules\v1\controllers;

use addons\Patrol\common\models\PatrolPlace;
use addons\Patrol\common\models\PatrolRecord;
use Yii;
use yii\data\ActiveDataProvider;
use common\enums\StatusEnum;
use api\controllers\OnAuthController;
use common\helpers\ResultHelper;
use yii\db\Expression;
use yii\helpers\Json;

/**

 * @property \yii\db\ActiveRecord|\yii\base\Model $modelClass;
 * @author 古月 <21931118@qq.com>
 */
class RecordController extends OnAuthController
{
    public $modelClass = PatrolRecord::class;

    /**
     * 不用进行登录验证的方法
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $authOptional = ['index', 'view'];

    /**
     * 首页
     *
     * @return ActiveDataProvider
     */
    public function actionIndex()
    {
//        if (Yii::$app->request->get('date') == '9999')
//            $where = '';
//        else {
//            $start = strtotime(Yii::$app->request->get('date') . ' 00:00:01');
//            $end = strtotime(Yii::$app->request->get('date') . ' 23:59:59');
//            $created_time = ['between', 'created_at', $start, $end];
//        }
        return new ActiveDataProvider([
            'query' => $this->modelClass::find()
//                ->andWhere($created_time)
                ->andWhere(['member_id' => Yii::$app->user->identity->member_id])
                ->andFilterWhere(['state' => Yii::$app->request->get('state')])
                ->orderBy('id desc')
                ->with('member','place','patrolClasses')
                ->asArray(),
            'pagination' => [
                'pageSize' => $this->pageSize,
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);
    }
    /**
     * 创建
     *
     * @return mixed|\yii\db\ActiveRecord
     */
    public function actionCreate()
    {
        /* @var $model \yii\db\ActiveRecord */
        $model = new $this->modelClass();
        $model->attributes = Yii::$app->request->post();
        $model->member_id = Yii::$app->user->identity->member_id;
        $model->merchant_id = Yii::$app->user->identity->merchant_id;

        $record = \addons\Patrol\common\models\PatrolRecord::find()->andWhere(['member_id' => Yii::$app->user->identity->member_id,'place_id'=>$model->place_id,'classes_id'=>$model->classes_id])->one();
        if($record&&date("Y-m-d", $record->created_at)==date("Y-m-d", time()))
            return ResultHelper::json(422, "该班次已经巡更过了，不需要重复巡更打卡！");
//        return $this->message("该班次已经巡更过了，不需要重复巡更打卡！", $this->redirect(Yii::$app->request->referrer), 'error');
        $where = new Expression("FIND_IN_SET(:supplier, check_date)", [":supplier" => date("Y-m-d", time())]);
        $plans = \addons\Patrol\common\models\PatrolPlan::find()->andWhere($where)->andWhere(['member_id' => Yii::$app->user->identity->member_id,'place_id'=>$model->place_id,'classes_id'=>$model->classes_id])->one();
        if(!$plans)
            return ResultHelper::json(422, "没有该班次！");
//        return $this->message("没有该班次！", $this->redirect(Yii::$app->request->referrer), 'error');
        $model->begin_time = $plans->begin_time;
        $model->end_time = $plans->end_time;

        if (!$model->save()) {
            return ResultHelper::json(422, $this->getError($model));
        }
        return $model;
    }
    public function actionView($id)
    {
        $model = $this->modelClass::find()->where(['id' => $id,'status' => StatusEnum::ENABLED,])
            ->with('member','place','classes')->asArray()->one();
        return $model;
    }

    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
        if (in_array($action, ['delete'])) {
            throw new \yii\web\BadRequestHttpException('权限不足');
        }
    }
}